package org.dftproject.genesis.ui.pages;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dftproject.genesis.ui.Activator;
import org.dftproject.genesis.ui.editors.TabPart;
import org.dftproject.genesis.ui.editors.TabPartEditorInput;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;

public class PageManager {

	private static final Log log = LogFactory.getLog(PageManager.class);
	
	public static final String PAGES_EXTENSION_POINT = "pages";
	
	private static PageManager singleton = new PageManager();
	
	private PageManager() {
	}
	
	public static PageManager getDefault() {
		return singleton;
	}
	
	public void openPage(IWorkbenchPage workbenchPage, IPageInput pageInput, String pageID, boolean activate, boolean fresh) {
		TabPart activeTabPart = null;
		
		if (!fresh) {
			// Try to get the active page editor for reuse
			
			IEditorPart activeEditor = workbenchPage.getActiveEditor();
			if (activeEditor instanceof TabPart)
				activeTabPart = (TabPart) activeEditor;
		}
		
		if (fresh || activeTabPart == null) {
			// Create a new page editor
			
			try {
				workbenchPage.openEditor(new TabPartEditorInput(pageInput, pageID), TabPart.ID, activate);
			} catch (PartInitException e) {
				e.printStackTrace(System.err);
			}
		} else {
			// Reuse the active page editor
			
			TabPartEditorInput tabPartEditorInput = activeTabPart.getTabPartEditorInput();
			tabPartEditorInput.setPageInput(pageInput);
			tabPartEditorInput.setPageID(pageID);
			activeTabPart.navigate();
		}
	}
	
	public static IPagePart createPagePart(String pageId) {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint extensionPoint = registry.getExtensionPoint(Activator.ID, PageManager.PAGES_EXTENSION_POINT);
		IExtension[] extensions = extensionPoint.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			IConfigurationElement[] configElements = extensions[i].getConfigurationElements();
			for (int j = 0; j < configElements.length; j++) {
				if (configElements[j].getAttribute("id").equals(pageId)) {
					try {
						Object object = configElements[j].createExecutableExtension("class");
						if (object instanceof IPagePart)
							return (IPagePart) object;
					} catch (CoreException e) {
						e.printStackTrace(System.err);
					}
				}
			}
		}
		
		log.error("Failed to create page part for id: " + pageId);
		return null;
	}

	public static TabPart getActiveTabPart(IWorkbenchPage workbenchPage) {
		IEditorPart activeEditor = workbenchPage.getActiveEditor();
		if (activeEditor instanceof TabPart)
			return (TabPart) activeEditor;
		return null;
	}

}
